#include "gamenegotiationdialog.h"
#include <QDialog>
#include <QMessageBox>
#include <QTextBrowser>
#include <QUrl>
#include <QtCore>

GameNegotiationDialog::GameNegotiationDialog(QWidget* parent,bool suggestgame, GameConfiguration* gc): QDialog(parent)
{
	ui.setupUi(this);
	setViewMode(suggestgame);
	m_pCurrentSuggestion = gc;
	connect(ui.sendBtn,SIGNAL(clicked()),this,SLOT(sendSuggestion()));
	connect(ui.noBtn,SIGNAL(clicked()),this,SLOT(declineOffer()));
	connect(ui.yesBtn,SIGNAL(clicked()),this,SLOT(acceptOffer()));
}

void GameNegotiationDialog::setViewMode(bool suggestgame)
{		
	if (suggestgame)
	{
		setSuggestState();
	}
	else
	{
		setWaitState();
		ui.yesBtn->setEnabled(false);
		ui.noBtn->setEnabled(false);
		ui.yesBtn->setVisible(false);
		ui.noBtn->setVisible(false);
	}
}

void GameNegotiationDialog::setSuggestState()
{
	ui.yesBtn->setEnabled(false);
	ui.noBtn->setEnabled(false);
	ui.yesBtn->setVisible(false);
	ui.noBtn->setVisible(false);
	ui.sendBtn->setEnabled(true);
	ui.sendBtn->setVisible(true);
	ui.gamekindBox->setEnabled(true);
	ui.parameterBox->setEnabled(true);
	ui.gamekindBox->setVisible(true);
	ui.parameterBox->setVisible(true);
	ui.waitLabel->setVisible(false);
}

void GameNegotiationDialog::setWaitState()
{
	ui.yesBtn->setEnabled(false);
	ui.noBtn->setEnabled(false);
	ui.yesBtn->setVisible(true);
	ui.noBtn->setVisible(true);
	ui.sendBtn->setEnabled(false);
	ui.sendBtn->setVisible(false);
	ui.gamekindBox->setEnabled(false);
	ui.parameterBox->setEnabled(false);
	ui.gamekindBox->setVisible(true);
	ui.parameterBox->setVisible(true);
	ui.waitLabel->setVisible(true);
}

void GameNegotiationDialog::receiveSuggestion(QString suggestion)
{
	ui.waitLabel->setVisible(false);
qDebug()<<"gamedialog got suggestion" << suggestion;
	m_pCurrentSuggestion->setConfiguration(suggestion);
	if (m_pCurrentSuggestion->getAccept() )
	{	
		// our suggestion was accepted.
		qDebug()<<"game was accepted";
		QMessageBox::information(this,"ACCEPT","Your game suggestion was accepted fast as a shark");
		emit SIG_finished();
		QDialog::done(0);
	}
	else if ( m_pCurrentSuggestion->getDecline())
	{
		qDebug()<<"game was declined";
		QMessageBox::information(this,"Decline","Your game suggestion was declined");
		//TODO, what do we do now? wait for other player to make suggestion?
		// who should start this? na, I just let clients do this.
		m_pCurrentSuggestion->setDecline(false);
		setWaitState();
	}
	else
	{
		qDebug()<<"got ordinary suggestion";
		if (m_pCurrentSuggestion->getVariant() == STANDARD)
			if (!ui.kindStandard->isChecked())
				ui.kindStandard->toggle();
		if (m_pCurrentSuggestion->getBert() )
			ui.parameterBert->setCheckState(Qt::Checked);
		else
			ui.parameterBert->setCheckState(Qt::Unchecked);
		if (m_pCurrentSuggestion->getTime())
		{
			ui.parameterTime->setCheckState(Qt::Checked);
			ui.parameterTimeValue->setCurrentIndex(m_pCurrentSuggestion->getTimeValue());
		}
		else
			ui.parameterTime->setCheckState(Qt::Unchecked);
		if (m_pCurrentSuggestion->getHandicap())
		{
			ui.parameterHandicap->setCheckState(Qt::Checked);
			ui.parameterHandicapValue->setCurrentIndex(m_pCurrentSuggestion->getHandicapValue());
		}
		else
			ui.parameterHandicap->setCheckState(Qt::Unchecked);
		if (m_pCurrentSuggestion->getFirst())
			ui.parameterFirst->setCheckState(Qt::Checked);
		else
			ui.parameterFirst->setCheckState(Qt::Unchecked);
		
		ui.yesBtn->setEnabled(true);
		ui.noBtn->setEnabled(true);
		ui.yesBtn->setVisible(true);
		ui.noBtn->setVisible(true);
	}
}
	
void GameNegotiationDialog::sendSuggestion()
{
	if (ui.kindStandard->isChecked())
		m_pCurrentSuggestion->setVariant(STANDARD);
	m_pCurrentSuggestion->setBert(ui.parameterBert->isChecked());
	m_pCurrentSuggestion->setTime(ui.parameterTime->isChecked());
	m_pCurrentSuggestion->setTimeValue(ui.parameterTimeValue->currentIndex());
	m_pCurrentSuggestion->setHandicap(ui.parameterHandicap->isChecked());
	m_pCurrentSuggestion->setHandicapValue(ui.parameterHandicapValue->currentIndex());
	m_pCurrentSuggestion->setFirst(ui.parameterFirst->isChecked());
	emit SIG_sendSuggestion(m_pCurrentSuggestion->generateDataToSend());
	ui.sendBtn->setEnabled(false);
}

void GameNegotiationDialog::declineOffer()
{
	emit SIG_sendSuggestion("DECLINE");
	setSuggestState();
}

void GameNegotiationDialog::acceptOffer()
{
	emit SIG_sendSuggestion("OK");
	emit SIG_finished();
	QDialog::done(0);
}

